package com.leetcode.LeetCode精选TOP面试题;

/**
 * @author 覃国强
 * @date 2022-05-22 17:41
 */
public class 题库_0041_缺失的第一个正数 {

  static
  class Solution {
    public int firstMissingPositive(int[] nums) {
      if (nums == null || nums.length == 0) {
        return 1;
      }
      for (int i = 0; i < nums.length; ++i) {
        while (nums[i] > 0 && nums[i] <= nums.length && nums[nums[i] - 1] != nums[i]) {
          int temp = nums[nums[i] - 1];
          nums[nums[i] - 1] = nums[i];
          nums[i] = temp;
        }
      }
      for (int i = 0; i < nums.length; ++i) {
        if (nums[i] != i + 1) {
          return i + 1;
        }
      }
      return nums.length + 1;
    }
  }

}
